# coding:utf-8
"""
    @author : 计祝兵
    @date : 2024/6/4 08:59
    @file : homework.py
    @desc : 作业
"""
from docx import Document
from docx.shared import Inches, RGBColor, Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_ALIGN_PARAGRAPH,WD_COLOR_INDEX
from docx.enum.style import WD_STYLE_TYPE

doc = Document()

style = doc.styles['Normal']
style.font.name = '微软雅黑'
style.font.size = Pt(15)

title = doc.add_heading('', 0)
# 标题居中
title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
#  标题字体的大小
title.style.font.size = Pt(22)
# 标题的内容
_t = title.add_run('XXX是谁')
# 加粗
_t.bold = True

# 第一个段落
p = doc.add_paragraph('我们是某某行业的')
color_p = p.add_run('领导者')
font = color_p.font
font.color.index = WD_COLOR_INDEX.RED
p.add_run('也是前沿技术的')
p.add_run('创造者').font.color.index = WD_COLOR_INDEX.RED
p.add_run(' and ')
p.add_run('传播者').font.color.index = WD_COLOR_INDEX.RED

# 第二个段落
p1 = doc.add_paragraph('体系课有\n')
p1.add_run('1、python课程\n')
p1.add_run('2、java课程\n')
p1.add_run('3、go课程')
p1.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT

# 第二页
doc.add_page_break()

# 设置表格
p3 = doc.add_paragraph()
p3.add_run('学生信息').font.size = Pt(23)

title = ['序号','姓名','年龄','身高']
table = doc.add_table(rows=1, cols=4, style='Colorful Shading Accent 6')
title_cells = table.rows[0].cells
title_cells[0].text = title[0]
title_cells[1].text = title[1]
title_cells[2].text = title[2]
title_cells[3].text = title[3]

data = [
    ('1', '张三', '20','174'),
    ('2', '李四', '19','167')
]
for d in data:
    rows_cell = table.add_row().cells
    rows_cell[0].text = d[0]  # 序号
    rows_cell[1].text = d[1]  # 姓名
    rows_cell[2].text = d[2]  # 年龄
    rows_cell[3].text = d[3]  # 身高

doc.save('demo.docx')


